﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using System.Linq;

using MRadio.Model;
using System.IO.IsolatedStorage;
using System.Collections.ObjectModel;

namespace MRadio
{
    /// <summary>
    /// 
    /// </summary>
    public class Global
    {
        private static List<FavoriteChannel> ChannelStting=new List<FavoriteChannel>();
        private const string SETTING_KEY = "SETTING_KEY";
        private const string SETTING_SHOW_GUID = "SETTING_SHOW_GUID";

        /// <summary>
        /// 取资源文件夹中的图片
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static BitmapImage GetImageByName(string fileName)
        {
            string asmName = System.Reflection.Assembly.GetExecutingAssembly().FullName.Split(',')[0];
            return new BitmapImage(new Uri(string.Format("/{0};component/Resources/Images/{1}", asmName, fileName),
                                                UriKind.Relative));
        }

        #region -- 频道管理 -- 

        /// <summary>
        /// 初始化频道收藏。
        /// </summary>
        public static void InitConfigSetting()
        {
            try
            {
                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                settings.TryGetValue<List<FavoriteChannel>>(SETTING_KEY, out ChannelStting);
            }
            catch
            {
                ;
            }
        }

        /// <summary>
        /// Gets or sets if show user guid.
        /// </summary>
        public static bool IsShowUserGuid
        {
            get
            {
                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                try
                {
                    if (settings.Contains(SETTING_SHOW_GUID))
                        return false; //settings[SETTING_SHOW_GUID]
                    else
                        return true;
                }
                catch {  return true;}
               
            }
            set
            {
                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                try
                {
                    settings[SETTING_SHOW_GUID] = value;
                }
                catch { }
            }
        }
        /// <summary>
        /// 新增频道。
        /// </summary>
        /// <param name="channel"></param>
        /// <returns>0:成功 1：已存在</returns>
        public static int AddChannel(FavoriteChannel channel)
        {
            int exists = ChannelStting.Where(o => o.Frequency == channel.Frequency).Count();
            if (exists > 0)
            {
                return 1;
            }
            ChannelStting.Insert(0, channel);
            SaveChannelConfig();
            return 0;
        }

        /// <summary>
        /// 是否存在收藏。
        /// </summary>
        /// <param name="frequency"></param>
        /// <returns></returns>
        public static bool IfExistsFavorite(float frequency)
        {
            int exists = ChannelStting.Where(o => o.Frequency == frequency).Count();
            if (exists > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///取频道。 
        /// </summary>
        /// <param name="frequency"></param>
        /// <returns></returns>
        public static string GetChannel(float frequency)
        {
            if (ChannelStting == null)
                return string.Empty;
            var obj = ChannelStting.Where(o => o.Frequency == frequency).FirstOrDefault();
            if (obj != null)
                return obj.ChannelName;
            else
                return string.Empty;
        }

        /// <summary>
        /// 移除频道收藏。
        /// </summary>
        /// <param name="channel"></param>
        public static void RemoveChannel(FavoriteChannel channel)
        {
            var obj = ChannelStting.Where(o => o.Id == channel.Id).FirstOrDefault();
            if (obj != null)
                ChannelStting.Remove(obj);
            SaveChannelConfig();
        }

        /// <summary>
        ///保存频道配置。 
        /// </summary>
        public static void SaveChannelConfig()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            settings[SETTING_KEY] = ChannelStting;
            settings.Save();
        }

        /// <summary>
        /// 获取频道配置信息
        /// </summary>
        public static ObservableCollection<FavoriteChannel> ChannelConfig
        {
            get
            {
                if (ChannelStting == null)
                    ChannelStting = new List<FavoriteChannel>();
                ObservableCollection<FavoriteChannel> config = new ObservableCollection<FavoriteChannel>(ChannelStting);
                return config;
            }
        }

        #endregion

        /// <summary>
        /// 收藏列表频道改变.
        /// </summary>
        public static Action<float> ChangeFavoriteChanne;
    }
}
